/*
 * UMLSynch plugin for Eclipse
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.EclipseOperationServer;

import java.util.ArrayList;
import java.util.List;


import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.core.resources.IFile;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

import com.googlecode.umlsynch.EclipseSearchVeiwWrapper.EclipseSeachQuery;
import com.googlecode.umlsynch.dialogs.FileSelectDialog;
import com.googlecode.umlsynch.dialogs.FilesSelectDialog;
import com.googlecode.umlsynch.eclipse.astutils.ASTUtils;
import com.googlecode.umlsynch.eclipse.astutils.TranslationUnitUtils;
import com.googlecode.umlsynch.eclipse.rpc.Debug.TEclipseDebug;
import com.googlecode.umlsynch.eclipse.views.DefinitionView;


public class TEclipseOperationServerImplementation2 implements
		IEclipseOperationServer {
	private TEclipseDebug debug = new TEclipseDebug("Impl2");
	private ASTUtils astutils;
	private TranslationUnitUtils tuUtils = TranslationUnitUtils.GetInstance();
	

	
	
	public TEclipseOperationServerImplementation2() {
		   debug.Note("Hello from Generic console sample action");
		   astutils = astutils.instance();
	}
	
	public void GetClassCField(VPSDEClassDescription clDesc) {
		
	}
	
	
	@Override
	public void OpenClassWithEditor(VPSDEClassDescription MainClass) {
		/*
		debug.Note("OpenClassFile");
		MainClass = astutils.IdentifyClass(MainClass);
		if (MainClass != null) {
			astutils.GetClassFile(MainClass);	
		} else {
			return;
		}
		
		DefinitionView inst = DefinitionView.GetInstance();
		if (inst != null) {
			debug.Note("Not null instance");
			inst.OpenFile(MainClass.projectName, MainClass.classFileName, 20);
		} else {
			debug.Note("Not instance");
		}*/

	}

	@Override
	public VPSDEClassDescription UpdateClassData(VPSDEClassDescription in) {
	/*	
		if ((in != null) && (in.className != null)) {
			VPSDEClassDescription ret = astutils.IdentifyClass(in);
			
			debug.Note("Update class");
			
			if (ret != null) {
				debug.Note("Update class");
				ret = tuUtils.UpdateClassData(in);
			}
			
			return (ret != null) ? ret:in;
		}
		*/
		return in;
	}

	@Override
	public void SearchTextPattern(VPSDESearchQueryDescription desc) {
		/*
		this.debug.Note("Searching: " + desc.projectName +
				"  " + desc.searchText + "   Type:" + desc.SearchType);
		
		EclipseSeachQuery sq = new EclipseSeachQuery(desc);
		sq.performAction();
		*/		
	}

	@Override
	public VPSDEClassDescription[] FindClassByName(String[] searchPaths,
			String searchNamePattern) {
				return null;
		/*	
		return tuUtils.FindClassByName(searchPaths, searchNamePattern);
		 */
	}

	@Override
	public String FindValueByName(String sourceFile,
			String namePattern) {
				return namePattern;
		/*
		return tuUtils.FindValuesByType(sourceFile, namePattern);
		*/
	
	}

	@Override
	public String FindValueByType(String sourceFile,
			String namePattern) {
				return namePattern;
		/*
		return tuUtils.FindValuesByType(sourceFile, namePattern);
		*/	
	}

	
	@Override
	public void OpenMethodImplementation(VPSDEClassDescription className,
			String methodName, String Desc) {
		/*
		debug.Debug("OpenMethodImplementation  " + methodName + "   " + Desc);
				
		if ((className.className != null)
				&& (methodName != null)) {
			debug.Debug("all not null");
			if (Desc == null)			
				astutils.OpenMethodImplementation(className, methodName, true);
			else if (Desc.equalsIgnoreCase("OpenDeclarationView")) {
				debug.Debug("OpenDeclarationView");
				DefinitionView inst = DefinitionView.GetInstance();
				if (inst != null) {
					debug.Debug("1");
					IASTFileLocation location = astutils.OpenMethodImplementation(className, methodName, false);
					
					if (location != null) {
						String fname = location.getFileName();
					    
						int start = location.getEndingLineNumber();
						int pos = fname.indexOf(className.projectName) + className.projectName.length() + 1;
						
						debug.Debug("Found:" + fname + " pos:" + start
								+ " node:" + location.getNodeLength());
						
						fname = fname.substring(pos, fname.length());
					
						inst.OpenFile(className.projectName, fname, start);
					} else {
						debug.Error("Method " + methodName + " location is null");
					}
				}
			} else if (Desc.equalsIgnoreCase("ProjectExplorer")) {
				// Focus in project explorer
			}
		}		*/
	}

	@Override
	public VPSDEClassDescription[] GetClassRelations(
			VPSDEClassDescription desc, String relationType,
			String[] searchRestrictions) {
				return null;
		/*
        // Identify class (open file select dialog, if necessary)
		desc = astutils.IdentifyClass(desc);
		if (desc == null) {
			return null;
		}
		debug.Note("Performe: " + relationType);
		// Find class relations		
		VPSDEClassDescription[] result = null;		                      
		if (relationType.equals("Base")) {
			result = astutils.GetClassBases(desc);
		}
		else if (relationType.equals("Generalization")) {
			result = astutils.GetClassGeneralizations(desc);
		}
		else if (relationType.equals("Dependency")
				|| relationType.equals("Aggregation")
				|| relationType.equals("Composition")
				|| relationType.equals("Association")) {
			result = astutils.GetClassRelation(desc, relationType, searchRestrictions);
		}
		else if (relationType.equals("Nested")
				|| relationType.equals("Containment")) {
			result = astutils.GetNestedClasses(desc);
		}
		else if (relationType.equals("Friend")) {
			result = astutils.GetClassFriends(desc);
		}
		
		// Select the resulted methods, and add 
		// an identified class to the top of the results
		
		List<VPSDEClassDescription> resultList = new ArrayList<VPSDEClassDescription>();
		// add an updated class element
		resultList.add(desc);

		if ((result != null) && (result.length > 0)) {

			ListChecker2 resultsSelector = new ListChecker2();

			resultsSelector.filesList = new String[result.length];
			resultsSelector.namesList = new String[result.length];

			for (int i=0; i<result.length; ++i) {
				resultsSelector.namesList[i] = result[i].className;
				resultsSelector.filesList[i] = 
					(result[i].classFileName == null) ? "unknown" : result[i].classFileName;
			}

			PlatformUI.getWorkbench().getDisplay().syncExec(resultsSelector);

			if (resultsSelector.selNamesList != null)
				for (int i=0; i<result.length; ++i) {
					for (int j=0; j<resultsSelector.selNamesList.length; ++j) {
						if ((resultsSelector.selNamesList[j] == result[i].className)
							&& ((resultsSelector.selFilesList[j] == result[i].classFileName) 
							|| ((resultsSelector.selFilesList[j] == "unknown") 
							&& (result[i].classFileName == null)))){
							resultList.add(result[i]);
							break;
						}
					}
				}
		} else {
			debug.Note("Result of " + relationType + " search is null");
		}
		return this.tuUtils.DynamcCastListToArray(resultList);
		*/
	}

	
}
